package com.yf.exam.modules.exam.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yf.base.api.api.dto.PagingReqDTO;
import com.yf.base.utils.DecimalUtils;
import com.yf.exam.modules.exam.dto.request.ExamRecordReqDTO;
import com.yf.exam.modules.exam.dto.response.ExamRecordRespDTO;
import com.yf.exam.modules.exam.entity.Exam;
import com.yf.exam.modules.exam.entity.ExamRecord;
import com.yf.exam.modules.exam.mapper.ExamRecordMapper;
import com.yf.exam.modules.exam.service.ExamRecordService;
import com.yf.exam.modules.exam.service.ExamService;
import com.yf.job.service.JobService;
import com.yf.system.modules.points.service.UserPointsService;
import com.yf.system.modules.user.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

/**
* <p>
* 考试记录业务实现类
* </p>
*
* @author 聪明笨狗
* @since 2020-09-21 15:13
*/
@Service
public class ExamRecordServiceImpl extends ServiceImpl<ExamRecordMapper, ExamRecord> implements ExamRecordService {



    @Autowired
    private ExamService examService;

    @Autowired
    private UserPointsService userPointsService;

    @Autowired
    private JobService jobService;


    @Override
    public IPage<ExamRecordRespDTO> myPaging(PagingReqDTO<ExamRecordReqDTO> reqDTO) {

        ExamRecordReqDTO params = reqDTO.getParams();


        if(params==null){
            params = new ExamRecordReqDTO();
        }

        params.setUserId(UserUtils.getUserId());


        //转换结果
        IPage<ExamRecordRespDTO> pageData = baseMapper.paging(reqDTO.toPage(), params);
        return pageData;
    }

    @Override
    public void joinResult(boolean review, String userId, String examId, BigDecimal score, boolean passed) {

        //查询条件
        QueryWrapper<ExamRecord> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(ExamRecord::getUserId, userId)
                .eq(ExamRecord::getExamId, examId);

        ExamRecord record = this.getOne(wrapper, false);
        if(record == null){
            record = new ExamRecord();
            record.setCreateTime(new Date());
            record.setUpdateTime(new Date());
            record.setUserId(userId);
            record.setExamId(examId);
            record.setMaxScore(score);
            record.setPassed(passed);
            record.setTryCount(1);
            this.save(record);
        }else {

            record.setUpdateTime(new Date());

            if ( DecimalUtils.lt(record.getMaxScore(), score)) {
                record.setMaxScore(score);
                record.setPassed(passed);
            }
            this.updateById(record);
        }

        // 考试通过
        if(passed){

            // 发放积分
            Exam exam = examService.getById(examId);
            if(exam.getPoints()!=null && exam.getPoints()>0){
                userPointsService.incr(userId, exam.getPoints(), "通过考试："+ exam.getTitle(), examId);
            }
        }

    }

    @Override
    public int countExam(String userId, String examId) {
        //查询条件
        QueryWrapper<ExamRecord> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(ExamRecord::getUserId, userId)
                .eq(ExamRecord::getExamId, examId);


        ExamRecord record = this.getOne(wrapper, false);

        if(record == null){
            return 0;
        }

        return record.getTryCount();

    }

    @Override
    public void increaseTryCount(String userId, String examId) {

        //查询条件
        QueryWrapper<ExamRecord> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(ExamRecord::getUserId, userId)
                .eq(ExamRecord::getExamId, examId);

        ExamRecord record = this.getOne(wrapper, false);
        if(record == null){
            record = new ExamRecord();
            record.setCreateTime(new Date());
            record.setUpdateTime(new Date());
            record.setUserId(userId);
            record.setExamId(examId);
            record.setMaxScore(DecimalUtils.zero());
            record.setPassed(false);
            record.setTryCount(1);
            this.save(record);
            return;
        }

        record.setTryCount(record.getTryCount() + 1);
        record.setUpdateTime(new Date());
        this.updateById(record);
    }
}
